﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class ExamRoomStudent
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();


            try
            {
                string ExamRoomId = (string)WebEnvironments.Request(SiteId, "ExamRoomId");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamRoomId);
                if (OExamRoom == null) throw new Exception("Tham số không hợp lệ!");

                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;

                bool CheckAllowAccess = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamRoomPermission.StaticId, ExamRoomPermission.funcAccess, ExamRoomId, UserId, false);
                if (!CheckAllowAccess)
                    throw new Exception(ExamUltility.AccessDenied);

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string RetHtml =
                #region Javascript
 " <script language=javascript> \r\n" +
                    "   function ClientImportStudentList(ExamRoomId)\r\n" +
                    "   {\r\n" +
                    "       window.open('ImportExamRoomStudent.aspx?ExamRoomId='+ExamRoomId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientAddExamRoomStudent(ExamRoomId)\r\n" +
                    "   {\r\n" +
                    "       window.open('AddExamRoomStudent.aspx?ExamRoomId='+ExamRoomId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientUpdateExamRoomStudent(ExamRoomId,ExamRoomStudentId)\r\n" +
                    "   {\r\n" +
                    "       window.open('UpdateExamRoomStudent.aspx?ExamRoomId='+ExamRoomId+'&ExamRoomStudentId='+ExamRoomStudentId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientSetExamRoomStudentSession(ExamRoomId,ExamRoomStudentId)\r\n" +
                    "   {\r\n" +
                    "       window.open('SetExamRoomStudentSession.aspx?ExamRoomId='+ExamRoomId+'&ExamRoomStudentId='+ExamRoomStudentId,'_self');\r\n" +
                    "   }\r\n" +


                    "   function ClientMakeExamRoomStudent(ExamRoomId)\r\n" +
                    "   {\r\n" +
                    "       window.open('MakeExamRoomStudent.aspx?ExamRoomId='+ExamRoomId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ListFinishedExamStudent(ExamRoomId)\r\n" +
                    "   {\r\n" +
                    "       window.open('ListFinishedExamStudent.aspx?ExamRoomId='+ExamRoomId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientPrint(ExamRoomId)\r\n" +
                    "   {\r\n" +
                    "       window.open('PrintExamStudent.aspx?ExamRoomId='+ExamRoomId,'_blank');\r\n" +
                    "   }\r\n" +


                    "  function ClientDeleteExamRoomStudent(ExamRoomId,ExamRoomStudentId)\r\n" +
                    "  {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                    "       Keyword=document.getElementById('txtKeyword').value;\r\n" +
                    "       AjaxOut=Portal.Exam.Render.ExamRoomStudent.ServerSideDelete(SiteId,ExamRoomId,ExamRoomStudentId,Keyword).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divExamRoomStudentMain').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "  }\r\n" +

                    "  function JumPageExamRoomStudent(PageIndex)\r\n" +
                    "  {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Exam.Render.ExamRoomStudent.GetHtml(SiteId,'" + ExamRoomId + "',PageIndex).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divExamRoomStudentMain').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "  }\r\n" +

                    " </script>\r\n" +

                #endregion

 " <input id=txtPageIndex type=hidden value=\"0\">\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"window.open('ExcelTemplate/ExamStudentList.xls','_blank');\"><img class=image src=\"" + IconRoot + "/DownloadTemplate.png\"><div>Download mẫu</div></li>\r\n";
                bool CheckAllowImportExam = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamRoomPermission.StaticId, ExamRoomPermission.funcImport, ExamRoomId, UserId, false);
                if (CheckAllowImportExam)
                {
                    RetHtml +=
                        "            <li onclick=\"javascript:ClientImportStudentList('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Import.png\"><div>Nhập từ tệp</div></li>\r\n";
                }
                bool CheckAllowManExam = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamRoomPermission.StaticId, ExamRoomPermission.funcManExam, ExamRoomId, UserId, false);
                if (CheckAllowManExam)
                {
                    RetHtml +=
                        "            <li onclick=\"javascript:ClientMakeExamRoomStudent('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Exam/MakeExam.png\"><div>Tạo đề thi</div></li>\r\n" +
                        "            <li onclick=\"javascript:ClientPrint('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Print.png\"><div>In đề thi</div></li>\r\n" +
                    "            <li onclick=\"javascript:ListFinishedExamStudent('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Exam/ExamResult.png\"><div>Kết quả thi</div></li>\r\n";
                }
                bool CheckAllowManStudent = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamRoomPermission.StaticId, ExamRoomPermission.funcManStudent, ExamRoomId, UserId, false);
                if (CheckAllowManStudent)
                {
                    RetHtml +=
                    "            <li onclick=\"javascript:ClientAddExamRoomStudent('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Add.png\"><div>Thêm mới</div></li>\r\n";
                }
                RetHtml +=
                    "            <li onclick=\"javascript:JumPageExamRoomStudent(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('ExamRoom.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "  <h3>Danh sách sinh viên trong phòng thi - " + OExamRoom.RoomNo + " / " + OExamRoom.ExamSubject + "</h3>\r\n" +
                    "  <div class=\"tabFunction\">Từ khóa: <input id=txtKeyword value=\"\"><input type=button class=button value=\"Lọc\" onclick=JumPageExamRoomStudent(0);></div>\r\n" +
                        "  <div id=divProcessing></div>" +
                        "  <div id=divExamRoomStudentMain class=tabcontent>" + GetHtml(SiteId, ExamRoomId, "", 0).HtmlContent + "</div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut GetHtml(
            string SiteId,
            string ExamRoomId,
            string Keyword,
            int PageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                bool CheckAllowManStudent = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, ExamRoomPermission.StaticId, ExamRoomPermission.funcManStudent, ExamRoomId, WebSessionUtility.GetCurrentLoginUser().UserId, false);

                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudent(SiteId, ExamRoomId, Keyword);
                if (dtExamRoomStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }
                string RetHtml =
                    "   <div class=PageTitle>Số sinh viên trong phòng: " + dtExamRoomStudent.Rows.Count.ToString("#,##0") + "</div>" +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th class=thIndex>#</th>" +
                    "           <th>Mã SV</th>" +
                    "           <th>Tên sinh viên</th>" +
                    //"           <th>Giới tính</th>" +
                    //"           <th>Ngày sinh</th>" +
                    //"           <th>Khóa</th>" +
                    "           <th>Lớp</th>" +
                    //"           <th>Ngành</th>" +
                    "           <th>Đề</th>" +
                    "           <th>Trạng thái</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>";

                for (int iIndex = 0; iIndex < dtExamRoomStudent.Rows.Count; iIndex++)
                {                    
                    string ExamRoomStudentId = (string)dtExamRoomStudent.Rows[iIndex]["ExamRoomStudentId"];
                    string StudentCode = (string)dtExamRoomStudent.Rows[iIndex]["StudentCode"];
                    string StudentName = (string)dtExamRoomStudent.Rows[iIndex]["StudentName"];
                    int Sex = (int)dtExamRoomStudent.Rows[iIndex]["Sex"];
                    int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    int HasGenQuestion = (int)dtExamRoomStudent.Rows[iIndex]["HasGenQuestion"];
                    DateTime StartExam = new System.DateTime();
                    if (dtExamRoomStudent.Rows[iIndex]["StartExam"] != DBNull.Value)
                    {
                        StartExam = (DateTime)dtExamRoomStudent.Rows[iIndex]["StartExam"];
                    }
                    string Status = "Chưa thi";
                    if (StartExam != new System.DateTime())
                    {
                        if (FinishExam == 0)
                        {
                            Status = "Đang thi";
                        }
                        else
                        {
                            Status = "Thi xong";
                        }
                    }

                    string sDob = "";
                    try
                    {
                        DateTime Dob = (DateTime)dtExamRoomStudent.Rows[iIndex]["Dob"];
                        sDob = Dob.ToString("dd/MM/yyyy");
                    }
                    catch { }
                    string Course = (string)dtExamRoomStudent.Rows[iIndex]["Course"];
                    string TrainingLevel = (string)dtExamRoomStudent.Rows[iIndex]["TrainingLevel"];
                    string Class = (string)dtExamRoomStudent.Rows[iIndex]["Class"];

                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td class=tdCode>" + StudentCode + "</td>" +
                    "           <td>" + StudentName + "</td>" +
                        //"           <td>" + (Sex==1?"Nam":"Nữ") + "</td>" +
                        //"           <td class=tdDate>" + sDob+ "</td>" +
                        //"           <td>" + Course + "</td>" +
                    "           <td>" + Class + "</td>" +
                        //"           <td>" + TrainingLevel + "</td>" +
                    "           <td>" + (HasGenQuestion == 1 ? "Đã tạo" : "Chưa tạo") + "</td>" +
                    "           <td>" + Status + "</td>";
                    if (CheckAllowManStudent)
                    {
                        RetHtml +=
                            "           <td class=tdButton><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdateExamRoomStudent('" + ExamRoomStudentId + "','" + ExamRoomStudentId + "');\"></td>" +
                            "           <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteExamRoomStudent('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                            "           <td class=tdButton><input type=button class=button value=\"Session\" onclick=\"javascript:ClientSetExamRoomStudentSession('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>";
                    }
                    else { 
                         RetHtml +=
                            "           <td class=tdButton></td>" +
                            "           <td class=tdButton></td>" +
                            "           <td class=tdButton></td>";
                    }
                    RetHtml +=
                    "       </tr>";
                }
                RetHtml +=
                    "   </table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamSegment(string SiteId, string ExamType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dt = new ExamSegmentBll().ReadingExamSegment(SiteId, ExamType, 1, "");
                string SelectExamSegment =
                    "<select id=drpSelectExamSegment>\r\n";
                for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
                {
                    string ExamSegmentId = (string)dt.Rows[iIndex]["ExamSegmentId"];
                    string ExamSegmentName = (string)dt.Rows[iIndex]["ExamSegmentName"];
                    SelectExamSegment += "    <option value=\"" + ExamSegmentId + "\">" + ExamSegmentName + "</option>\r\n";
                }
                SelectExamSegment += "</select>\r\n";
                OAjaxOut.HtmlContent = SelectExamSegment;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string GetSetActiveButton(string SiteId, string ExamRoomStudentId)
        {
            string Html =
                "<input type=button class=button value=\"Sử dụng\" onclick=\"javascript:ClientStatus('" + ExamRoomStudentId + "',1);\">";
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public static string GetSetInActiveButton(string SiteId, string ExamRoomStudentId)
        {
            string Html =
                "<input type=button class=button value=\"Ngưng\" onclick=\"javascript:ClientStatus('" + ExamRoomStudentId + "',0);\">";
            return Html;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut GetAddExamUrl(string SiteId, string ExamSegmentId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(ExamSegmentId)) throw new Exception("Chưa xác định kỳ thi!");
                OAjaxOut.RetUrl = "AddExamRoomStudent.aspx?ExamSegmentId=" + ExamSegmentId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg(string SiteId)
        {
            return "Bạn đã chắc chắn xóa chưa?";
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteExamRoomStudent(string SiteId, string ExamRoomStudentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteExamRoomStudent(string SiteId, string ExamRoomStudentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamRoomStudentBll().DeleteExamRoomStudent(null, SiteId, ExamRoomStudentId);
                return "Xóa thành công";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUpdateExamUrl(string SiteId, string ExamRoomStudentId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string UpdateUrl = "UpdateExamRoomStudent.aspx?ExamRoomStudentId=" + ExamRoomStudentId;
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {

            return "Đang đọc dữ liệu từ máy chủ";
        }



        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideDelete(
            string SiteId,
            string ExamRoomId,
            string ExamRoomStudentId,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            OAjaxOut.InfoMessage = "";
            try
            {
                new ExamRoomStudentBll().DeleteExamRoomStudent(null, SiteId, ExamRoomStudentId);
                OAjaxOut.HtmlContent = GetHtml(SiteId, ExamRoomId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
